package com.dgut.courses_selecting_sys.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQDelayQueueConfig {

    /**定义广播类型交换机**/
    private static String MY_NORMAL_EXCHANGE="my_normal_exchange";
    /**活动延迟队列名*/
    private static String ACTIVITIES_DELAY_QUEUE = "activities_delay_queue";
    /**课程开选设置队列名*/
    private static String COURSE_TO_CHOICE_DELAY_QUEUE = "course_to_choice_delay_queue";
    /**课程开选关闭队列名*/
    private static String COURSE_TO_ING_DELAY_QUEUE = "course_to_ing_delay_queue";
    /**延迟队列(死信队列)交换器名*/
    private static String DLX_EXCHANGE = "dlx_exchange";
    /**活动处理业务的队列(死信队列)*/
    private static String ACTIVITIES_PROCESS_QUEUE = "activities_process_queue";
    /**课程开选设置队列名*/
    private static String COURSE_TO_CHOICE_DELAY_PROCESS_QUEUE = "course_to_choice_delay_process_queue";
    /**课程开选关闭队列名*/
    private static String COURSE_TO_ING_DELAY_PROCESS_QUEUE = "course_to_ing_delay_process_queue";
    /**活动路由key**/
    private static String ACTIVITY_ROUTING_KEY="activity_routing_key";
    /**课程开选路由key**/
    private static String COURSE_CHOICE_ROUTING_KEY="course_choice_routing_key";
    /**课程进行路由key**/
    private static String COURSE_ING_ROUTING_KEY="course_ing_routing_key";



    //死信交换机
    @Bean
    public DirectExchange dlx_exchange(){
        return ExchangeBuilder.directExchange(DLX_EXCHANGE).build();
    }


    //定义定向交换机中的持久化死信队列
    @Bean
    public Queue activities_process_queue(){
        return QueueBuilder.durable(ACTIVITIES_PROCESS_QUEUE).build();
    }
    @Bean
    public Binding dlx_exchange_to_queue(@Qualifier("activities_process_queue")Queue activities_process_queue,
                                         @Qualifier("dlx_exchange")DirectExchange dlx_exchange){
        //绑定路由键activity_routing_key，可以将过期的消息转移到activities_delay_queue队列
        return BindingBuilder.bind(activities_process_queue).to(dlx_exchange).with(ACTIVITY_ROUTING_KEY);
    }





    @Bean
    public Queue course_to_choice_delay_process_queue(){
        return QueueBuilder.durable(COURSE_TO_CHOICE_DELAY_PROCESS_QUEUE).build();
    }
    @Bean
    public Binding dlx_exchange_to_course_choice_queue(@Qualifier("course_to_choice_delay_process_queue")Queue course_to_choice_delay_process_queue,
                                         @Qualifier("dlx_exchange")DirectExchange dlx_exchange){
        //绑定路由键activity_routing_key，可以将过期的消息转移到activities_delay_queue队列
        return BindingBuilder.bind(course_to_choice_delay_process_queue).to(dlx_exchange).with(COURSE_CHOICE_ROUTING_KEY);
    }




    @Bean
    public Queue course_to_ing_delay_process_queue(){
        return QueueBuilder.durable(COURSE_TO_ING_DELAY_PROCESS_QUEUE).build();
    }
    @Bean
    public Binding dlx_exchange_to_course_ing_queue(@Qualifier("course_to_ing_delay_process_queue")Queue course_to_ing_delay_process_queue,
                                         @Qualifier("dlx_exchange")DirectExchange dlx_exchange){
        //绑定路由键activity_routing_key，可以将过期的消息转移到activities_delay_queue队列
        return BindingBuilder.bind(course_to_ing_delay_process_queue).to(dlx_exchange).with(COURSE_ING_ROUTING_KEY);
    }




    //定义定向交换机 根据不同的路由key投递消息
    @Bean
    public DirectExchange my_normal_exchange(){
        return ExchangeBuilder.directExchange(MY_NORMAL_EXCHANGE).build();
    }



    //定义过期队列及其属性
    @Bean
    public Queue activities_delay_queue(){
        return QueueBuilder.durable(ACTIVITIES_DELAY_QUEUE)
                .deadLetterExchange(DLX_EXCHANGE)//设置当消息过期后投递到对应的死信交换机
                .build();
    }
    @Bean
    public Binding normal_exchange_to_activities_queue(@Qualifier("activities_delay_queue")Queue activities_delay_queue,
                                                       @Qualifier("my_normal_exchange")DirectExchange my_normal_exchange){
        //绑定路由键my_ttl_dlx，可以将过期的消息转移到my_dlx_queue队列
        return BindingBuilder.bind(activities_delay_queue).to(my_normal_exchange).with(ACTIVITY_ROUTING_KEY);
    }



    @Bean
    public Queue course_to_choice_delay_queue(){
        return QueueBuilder.durable(COURSE_TO_CHOICE_DELAY_QUEUE)
                .deadLetterExchange(DLX_EXCHANGE)//设置当消息过期后投递到对应的死信交换机
                .build();
    }
    @Bean
    public Binding normal_exchange_to_activities_course_to_choice_queue(@Qualifier("course_to_choice_delay_queue")Queue course_to_choice_delay_queue,
                                                       @Qualifier("my_normal_exchange")DirectExchange my_normal_exchange){
        //绑定路由键my_ttl_dlx，可以将过期的消息转移到my_dlx_queue队列
        return BindingBuilder.bind(course_to_choice_delay_queue).to(my_normal_exchange).with(COURSE_CHOICE_ROUTING_KEY);
    }


    @Bean
    public Queue course_to_ing_delay_queue(){
        return QueueBuilder.durable(COURSE_TO_ING_DELAY_QUEUE)
                .deadLetterExchange(DLX_EXCHANGE)//设置当消息过期后投递到对应的死信交换机
                .build();
    }
    @Bean
    public Binding normal_exchange_to_activities_course_to_ing_queue(@Qualifier("course_to_ing_delay_queue")Queue course_to_ing_delay_queue,
                                                                        @Qualifier("my_normal_exchange")DirectExchange my_normal_exchange){
        //绑定路由键my_ttl_dlx，可以将过期的消息转移到my_dlx_queue队列
        return BindingBuilder.bind(course_to_ing_delay_queue).to(my_normal_exchange).with(COURSE_ING_ROUTING_KEY);
    }

}